/****************************************************************************
**
** ConfigManager.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
void TConfigManager::Init()
{
	DefaultPage1();

	DefaultPage2();
	
	DefaultPage3();

	DefaultPage4();

	DefaultPage6();

	DefaultPage7();
	
	DefaultPage8();
	
	DefaultPage9();

	m_FilterPWOn = false;
	m_ObscenityFilter = false;
	m_FilterPassword = "";
	
	m_GameWindowPosX = 0;
	m_GameWindowPosY = 0;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage1()
{
	m_Sound = true;
	m_Music = true;
	m_FootstepsSound = true;
	m_CombatMusic = true;
	m_SoundVolume = 255;
	m_MusicVolume = 255;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage2()
{
	m_PopupHelpEnabled = true;
	m_PopupHelpSticky = false;
	m_PopupHelpPersistant = true;
	m_PopupHelpDelay = 1000;
	m_PopupHelpTextColor = 0xFFFF;
	m_PopupHelpTextFont = 1;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage3()
{
	m_UseToolTips = true;
	m_ToolTipsTextColor = 0xFFFF;
	m_ToolTipsTextFont = 1;
	m_ToolTipsDelay = 1000;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage4()
{
	m_ChatColorInputText = 0xFFFF;
	m_ChatColorMenuOption = 0xFFFF;
	m_ChatColorPlayerInMemberList = 0xFFFF;
	m_ChatColorText = 0xFFFF;
	m_ChatColorPlayerNameWithout = 0xFFFF;
	m_ChatColorMuted = 0xFFFF;
	m_ChatColorChannelModeratorName = 0xFFFF;
	m_ChatColorChannelModeratorText = 0xFFFF;
	m_ChatColorMyName = 0xFFFF;
	m_ChatColorMyText = 0xFFFF;
	m_ChatColorSystemMessage = 0xFFFF;
	m_ChatColorBGOutputText = 0xFFFF;
	m_ChatColorBGInputText = 0xFFFF;
	m_ChatColorBGUserList = 0xFFFF;
	m_ChatColorBGConfList = 0xFFFF;
	m_ChatColorBGCommandList = 0xFFFF;
	
	m_ChatFont = 2;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage6()
{
	m_EnablePathfind = true;
	m_HoldTabForCombat = true;
	m_OffsetInterfaceWindows = true;
	m_AutoArrange = true;
	m_AlwaysRun = false;
	m_DisableMenubar = true;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage7()
{
	m_GameWindowSizeX = 800;
	m_GameWindowSizeY = 600;
	m_SpeechDelay = 100;
	m_ScaleSpeechDelay = true;
	m_SpeechColor = 0x02B2;
	m_EmoteColor = 0x0021;
	m_PartyMessageColor = 0x0044;
	m_DarkNights = false;
	m_ColoredLighting = true;
	m_StandartCharactersAnimationDelay = true;
	m_StandartItemsAnimationDelay = true;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage8()
{
	m_InnocentColor = 0x005A;
	m_FriendlyColor = 0x0044;
	m_SomeoneColor = 0x03B2;
	m_CriminalColor = 0x03B2;
	m_EnemyColor = 0x002B;
	m_MurdererColor = 0x0023;
	m_CriminalActionsQuery = true;
}
//---------------------------------------------------------------------------
void TConfigManager::DefaultPage9()
{
	m_ShowIncomingNames = true;
	m_UseCircleTrans = false;
	m_StatReport = true;
	m_ConsoleNeedEnter = false;
	m_CircleTransRadius = 64;
	m_SkillReport = 1;
	m_SpeechFont = 0;
}
//---------------------------------------------------------------------------
void TConfigManager::SetConsoleNeedEnter(bool val)
{
	m_ConsoleNeedEnter = val;
	//if (m_ConsoleNeedEnter && g_EntryPointer == NULL) g_EntryPointer = UO->m_GameConsole;
	if (m_ConsoleNeedEnter && g_EntryPointer == UO->m_GameConsole) g_EntryPointer = NULL;
}
//---------------------------------------------------------------------------
int TConfigManager::LoadConfig(FILE *file, long FileSize, BYTE VersionMaj, BYTE VersionMin)
{
	int ConfigSize = 107 + m_FilterPassword.length();

	if (VersionMin > 1) ConfigSize--;

	if (FileSize < ConfigSize + 2) return 0;

	BYTE b = 0;
	fread(&b, 1, 1, file);
	m_Sound = (b != 0);
	fread(&b, 1, 1, file);
	m_Music = (b != 0);
	fread(&b, 1, 1, file);
	m_FootstepsSound = (b != 0);
	fread(&b, 1, 1, file);
	m_CombatMusic = (b != 0);
	fread(&m_SoundVolume, 1, 1, file);
	fread(&m_MusicVolume, 1, 1, file);
	
	fread(&b, 1, 1, file);
	m_PopupHelpEnabled = (b != 0);
	fread(&b, 1, 1, file);
	m_PopupHelpSticky = (b != 0);
	fread(&b, 1, 1, file);
	m_PopupHelpPersistant = (b != 0);
	fread(&m_PopupHelpDelay, 2, 1, file);
	fread(&m_PopupHelpTextColor, 2, 1, file);
	fread(&m_PopupHelpTextFont, 2, 1, file);
	
	fread(&b, 1, 1, file);
	m_UseToolTips = (b != 0);
	fread(&m_ToolTipsTextColor, 2, 1, file);
	fread(&m_ToolTipsTextFont, 2, 1, file);
	fread(&m_ToolTipsDelay, 2, 1, file);
	
	fread(&m_ChatColorInputText, 2, 1, file);
	fread(&m_ChatColorMenuOption, 2, 1, file);
	fread(&m_ChatColorPlayerInMemberList, 2, 1, file);
	fread(&m_ChatColorText, 2, 1, file);
	fread(&m_ChatColorPlayerNameWithout, 2, 1, file);
	fread(&m_ChatColorMuted, 2, 1, file);
	fread(&m_ChatColorChannelModeratorName, 2, 1, file);
	fread(&m_ChatColorChannelModeratorText, 2, 1, file);
	fread(&m_ChatColorMyName, 2, 1, file);
	fread(&m_ChatColorMyText, 2, 1, file);
	fread(&m_ChatColorSystemMessage, 2, 1, file);
	fread(&m_ChatFont, 2, 1, file);
	fread(&m_ChatColorBGOutputText, 2, 1, file);
	fread(&m_ChatColorBGInputText, 2, 1, file);
	fread(&m_ChatColorBGUserList, 2, 1, file);
	fread(&m_ChatColorBGConfList, 2, 1, file);
	fread(&m_ChatColorBGCommandList, 2, 1, file);
	
	fread(&b, 1, 1, file);
	m_EnablePathfind = (b != 0);
	fread(&b, 1, 1, file);
	m_HoldTabForCombat = (b != 0);
	fread(&b, 1, 1, file);
	m_OffsetInterfaceWindows = (b != 0);
	fread(&b, 1, 1, file);
	m_AutoArrange = (b != 0);
	fread(&b, 1, 1, file);
	m_AlwaysRun = (b != 0);
	if (VersionMin > 1)
	{
		fread(&b, 1, 1, file);
		m_DisableMenubar = (b != 0);
	}
	else m_DisableMenubar = true;
	
	fread(&m_GameWindowSizeX, 2, 1, file);
	fread(&m_GameWindowSizeY, 2, 1, file);
	fread(&m_SpeechDelay, 2, 1, file);
	fread(&m_ScaleSpeechDelay, 2, 1, file);
	fread(&m_SpeechColor, 2, 1, file);
	fread(&m_EmoteColor, 2, 1, file);
	fread(&m_PartyMessageColor, 2, 1, file);
	fread(&b, 1, 1, file);
	m_DarkNights = (b != 0);
	fread(&b, 1, 1, file);
	m_ColoredLighting = (b != 0);
	fread(&b, 1, 1, file);
	m_StandartCharactersAnimationDelay = (b != 0);
	fread(&b, 1, 1, file);
	m_StandartItemsAnimationDelay = (b != 0);
	
	fread(&m_InnocentColor, 2, 1, file);
	fread(&m_FriendlyColor, 2, 1, file);
	fread(&m_SomeoneColor, 2, 1, file);
	fread(&m_CriminalColor, 2, 1, file);
	fread(&m_EnemyColor, 2, 1, file);
	fread(&m_MurdererColor, 2, 1, file);
	fread(&b, 1, 1, file);
	m_CriminalActionsQuery = (b != 0);
	
	if (VersionMin == 1)
	{
		fread(&b, 1, 1, file);
		fread(&b, 1, 1, file);
	}
	fread(&b, 1, 1, file);
	m_ShowIncomingNames = (b != 0);
	fread(&b, 1, 1, file);
	m_UseCircleTrans = (b != 0);
	fread(&b, 1, 1, file);
	m_StatReport = (b != 0);
	fread(&b, 1, 1, file);
	m_ConsoleNeedEnter = (b != 0);
	fread(&m_CircleTransRadius, 1, 1, file);
	fread(&m_SkillReport, 1, 1, file);
	fread(&m_SpeechFont, 2, 1, file);
	
	fread(&b, 1, 1, file);
	m_FilterPWOn = (b != 0);
	fread(&b, 1, 1, file);
	m_ObscenityFilter = (b != 0);
	WORD w = 0;
	fread(&w, 2, 1, file);
	if (w > 1)
	{
		char buf[50] = {0};
		fread(&buf, w, 1, file);

		m_FilterPassword = buf;
	}
	
	fread(&m_GameWindowPosX, 2, 1, file);
	fread(&m_GameWindowPosY, 2, 1, file);

	return ConfigSize;
}
//---------------------------------------------------------------------------
void TConfigManager::SaveConfig(FILE *file)
{
	BYTE b = (BYTE)m_Sound;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_Music;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_FootstepsSound;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_CombatMusic;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_SoundVolume;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_MusicVolume;
	fwrite(&b, 1, 1, file);
	
	b = (BYTE)m_PopupHelpEnabled;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_PopupHelpSticky;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_PopupHelpPersistant;
	fwrite(&b, 1, 1, file);
	WORD w = (WORD)m_PopupHelpDelay;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_PopupHelpTextColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_PopupHelpTextFont;
	fwrite(&w, 2, 1, file);
	
	b = (BYTE)m_UseToolTips;
	fwrite(&b, 1, 1, file);
	w = (WORD)m_ToolTipsTextColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ToolTipsTextFont;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ToolTipsDelay;
	fwrite(&w, 2, 1, file);
	
	w = (WORD)m_ChatColorInputText;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorMenuOption;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorPlayerInMemberList;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorText;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorPlayerNameWithout;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorMuted;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorChannelModeratorName;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorChannelModeratorText;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorMyName;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorMyText;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorSystemMessage;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatFont;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorBGOutputText;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorBGInputText;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorBGUserList;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorBGConfList;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ChatColorBGCommandList;
	fwrite(&w, 2, 1, file);
	
	b = (BYTE)m_EnablePathfind;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_HoldTabForCombat;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_OffsetInterfaceWindows;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_AutoArrange;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_AlwaysRun;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_DisableMenubar;
	fwrite(&b, 1, 1, file);
	
	w = (WORD)m_GameWindowSizeX;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_GameWindowSizeY;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_SpeechDelay;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_ScaleSpeechDelay;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_SpeechColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_EmoteColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_PartyMessageColor;
	fwrite(&w, 2, 1, file);
	b = (BYTE)m_DarkNights;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_ColoredLighting;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_StandartCharactersAnimationDelay;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_StandartItemsAnimationDelay;
	fwrite(&b, 1, 1, file);
	
	w = (WORD)m_InnocentColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_FriendlyColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_SomeoneColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_CriminalColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_EnemyColor;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_MurdererColor;
	fwrite(&w, 2, 1, file);
	b = (BYTE)m_CriminalActionsQuery;
	fwrite(&b, 1, 1, file);
	
	b = (BYTE)m_ShowIncomingNames;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_UseCircleTrans;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_StatReport;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_ConsoleNeedEnter;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_CircleTransRadius;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_SkillReport;
	fwrite(&b, 1, 1, file);
	w = (WORD)m_SpeechFont;
	fwrite(&w, 2, 1, file);
	
	b = (BYTE)m_FilterPWOn;
	fwrite(&b, 1, 1, file);
	b = (BYTE)m_ObscenityFilter;
	fwrite(&b, 1, 1, file);
	w = (WORD)m_FilterPassword.length();
	fwrite(&w, 2, 1, file);
	if (w) fwrite(&((char*)m_FilterPassword.c_str())[0], w, 1, file);
	
	w = (WORD)m_GameWindowPosX;
	fwrite(&w, 2, 1, file);
	w = (WORD)m_GameWindowPosY;
	fwrite(&w, 2, 1, file);
}
//---------------------------------------------------------------------------